package com.learn.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learn.entity.Banner;
import com.learn.mapper.BannerMapper;
import com.learn.service.BannerService;
import com.learn.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


/**
 * 轮播图服务实现类
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {


    @Autowired
    private FileUploadService fileUploadService;

    @Override
    public String uploadBannerImage(MultipartFile file) throws Exception {
        //1.非空校验
        if (file == null || file.isEmpty()){
            throw new RuntimeException("上传轮播图图片失败！原因：上传的文件为空！");
        }
        //2.文件类型格式校验 image -> mimetype image/png gif jpeg
        String contentType = file.getContentType();
        if ( ObjectUtils.isEmpty(contentType ) || !contentType.startsWith("image")){
            throw new RuntimeException("上传轮播图图片失败！原因：上传的文件类型错误,非图片！");
        }
        //3.调用核心业务
        String url = fileUploadService.uploadFile("banners", file);
        //4.返回地址
        return url;
    }

}